﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Common.NPOI
{
    /// <summary>
    /// 表格上下文对象
    /// </summary>
    public class ExcelSheetContext
    {
        /// <summary>
        /// 对应table列名或者DTO属性名
        /// </summary>
        public string ColumnName { get; set; }
        /// <summary>
        /// 映射表头名称
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// EXCEL列顺序，从小到大
        /// </summary>
        public int Order { get; set; }
        /// <summary>
        /// 是否忽略
        /// </summary>
        public bool Ignore { get; set; }
        /// <summary>
        /// 转换单元格值
        /// </summary>
        public Func<object, object> Converter;

        internal object Convert(object value)
        {
            if (Converter != null)
            {
                try
                {
                    return Converter(value);
                }
                catch
                {
                    return value;
                }
            }
            return value;
        }
        /// <summary>
        /// 创建上下文对象
        /// </summary>
        /// <param name="columnName">数据库表列名</param>
        /// <param name="column_title">列名</param>
        /// <param name="column_index">表格列顺序</param>
        /// <param name="ignore">是否忽略</param>
        /// <param name="converter">值转换委托</param>
        /// <returns></returns>
        public static ExcelSheetContext GetContext(string columnName, string column_title, int column_index, bool ignore = false, Func<object, object> converter = null)
        {
            var context = new ExcelSheetContext
            {
                ColumnName = columnName,
                Title = column_title,
                Order = column_index,
                Ignore = ignore,
                Converter = converter
            };
            return context;
        }
    }
}
